package oceanwiki.controller;

import oceanwiki.req.DocQueryReq;
import oceanwiki.req.DocSaveReq;
import oceanwiki.resp.CommonResp;
import oceanwiki.resp.DocQueryResp;
import oceanwiki.resp.PageResp;
import oceanwiki.service.IDocService;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import springfox.documentation.spring.web.plugins.Docket;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XBJ
 * @since 2024-09-07
 */
@RestController
@RequestMapping("/doc")
public class DocController {

    @Autowired
    IDocService docService;

    @GetMapping("list")
    public CommonResp list(@Valid DocQueryReq req) {
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>(true, "查询成功", null);
        PageResp<DocQueryResp> pageResp = docService.listByName(req);
        resp.setContent(pageResp);
        return resp;
    }

    @GetMapping("/all/{ebookId}")
    public CommonResp all(@PathVariable Long ebookId) {
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>(true, "查询成功", null);

        List<DocQueryResp> list = docService.all(ebookId);
        resp.setContent(list);

        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody DocSaveReq req) {
        CommonResp resp = new CommonResp(true, "成功", null);
        docService.save(req);
        return resp;
    }


    @GetMapping("/delete")
    public CommonResp delete(@RequestParam("ids") List<Long> ids) {
        CommonResp resp = new CommonResp(true, "删除成功", null);

        docService.delete(ids);
        return resp;
    }

    //点赞
    @GetMapping("/vote/{id}")
    public CommonResp vote(@PathVariable Long id) {
        CommonResp commonResp = new CommonResp();
        docService.vote(id);
        return commonResp;
    }
}
